import {ColorStr} from "../../type/Type";
import MathUtil from "./MathUtil";

export class CodeUtil {
    static isNullOrZero(value: any) {
        if (value === null || value === undefined) {
            return true;
        }
        if (value === 0) {
            return true; // 0 is not nil
        }
        if (CodeUtil.trim(value) === '') {
            return true;
        }
        return false; // other falsy values are nil, including NaN
    }
    static isNull(value: any) {
        if (value === null || value === undefined) {
            return true;
        }
        if (value === '') {
            return true;
        }
        return false; // other falsy values are nil, including NaN
    }

    static trim(str: string) {
        if (str) {
            if (typeof str === 'string') {
                let start = 0;
                let end = str.length - 1;

                // 找到开头的第一个非空格字符的位置
                for (; start <= end && str.charCodeAt(start) <= 32; start++) {}

                // 找到结尾的第一个非空格字符的位置
                for (; end >= start && str.charCodeAt(end) <= 32; end--) {}

                return start > end ? '' : str.slice(start, end + 1);
            }
            return str;
        }
        return '';
    }
    static getArrBySplit(str: string | number) {
        let arr: string[] = [];
        if (!CodeUtil.isNull(str)) {
            if (typeof(str)==='string') {
                if (str.indexOf(',') >= 0) {
                    arr = str.split(',');
                } else {
                    arr.push(str);
                }
            } else if (!CodeUtil.isNull(str)) {
                arr.push(str + '');
            }
        }
        return arr
    }

    // 返回
    static mapObj<T>(object: { [key: string]: T }, fun: (key: string, value: T) => any) {
        if (object) {
            const keys = Object.keys(object);
            for (let i = 0; i < keys.length; i ++) {
                const key = keys[i];
                const value = object[key];
                const ret = fun(key, value);
                if (ret) {
                    return;
                }
            }
        }
    }

    static getObjSize<T>(object: { [key: string]: T }) {
        let size = 0;
        CodeUtil.mapObj(object, (key, value) => {
            if (value) {
                size = size + 1
            }
        })
        return size
    }

    static getColorStr(color: ColorStr, text: string) {
        if (text) {
        } else {
            return '';
        }
        if (color) {
            return `${color}${text}|r`
        }
        return text
    }

    static copyValidKey(copyed: any, ret: any) {
        const keys = Object.keys(copyed);
        for (let i = 0; i < keys.length; i++) {
            const key: string = keys[i] as string;
            const value = copyed[key];
            ret[key] = value;
        }
    }
    static replaceAttrStr(oldStr: string, replaceFun: (attr: string) => number) {
        const left = '{';
        const right = '}';

        let tempArr = [];
        if (oldStr) {
            tempArr = oldStr.split(right);
        }
        let bChange = false;

        for (let i = 0; i < tempArr.length; i++) {
            let indexStr = tempArr[i];
            const index = indexStr.indexOf(left);
            if (index && index >= 0) {
                bChange = true;
                const replaceStr: string = indexStr.substring(index + 1, indexStr.length);
                const retStr = replaceFun(replaceStr);
                const indexStrArr = indexStr.split(left);
                indexStrArr[1] = ' ' + retStr
                tempArr[i] = indexStrArr.join('')
            }
        }
        if (bChange) {
            const ret = tempArr.join('');
            return ret;
        }
    }
    static inArr<T>(arr: T[], element: T) {
        const index = arr.indexOf(element);
        if (index >= 0) {
            return true
        }
    }

    static autoLineBreak(str: string, width: number, fontSize: number): string[] {
        const retLineArr = str.split('\n');
        // let l = 0;
        for (let i = retLineArr.length - 1; i >= 0; i--) {
            const str = retLineArr[i];
            if (str) {
                let textLen = str.length
                const hasColor = str.indexOf('|r');
                if (hasColor >= 0) {
                    textLen = textLen - 12;
                }
                let len = textLen;
                const realWidth = fontSize * len * 0.4;
                let temp = (realWidth / width);
                let int = MathUtil.floor(temp);
                // l = l + (temp - int);
                // if (l > 1) {
                //     int = int + 1;
                //     l = l - 1;
                // }
                for (let i = 0; i < int; i++) {
                    retLineArr.push('\n')
                }
            }
        }
        return retLineArr;
    }
}